package com.yuanfuxing.boxuegu.fragment;

import android.app.Activity;
import android.app.Fragment;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.annotation.Nullable;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.google.gson.Gson;
import com.yuanfuxing.boxuegu.R;
import com.yuanfuxing.boxuegu.activity.CourseModel;
import com.yuanfuxing.boxuegu.activity.VideoListActivity;

import org.xutils.common.Callback;
import org.xutils.http.RequestParams;
import org.xutils.x;

import java.util.ArrayList;


import static com.yuanfuxing.boxuegu.R.id.pager;

/**
 * Created by Administrator on 2018/4/2.
 */

public class CourseFragment extends Fragment {
    RecyclerView mRecyclerView;
    ViewPager mViewPager;
    int curent = 0;
    public ArrayList<CourseModel.Course> courses = new ArrayList<>();
    public ArrayList<ImageView> mViewPagerImgs = new ArrayList<>();
    Handler handler = new Handler(new Handler.Callback(){

        @Override
        public boolean handleMessage(Message message) {
            handler.sendEmptyMessageDelayed(1,1000);
            curent ++;
            mViewPager.setCurrentItem(curent % mViewPagerImgs.size());
            return false;
        }
    });
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, Bundle savedInstanceState) {
       View view =  inflater.inflate(R.layout.fragment_course,container,false);
        mRecyclerView = view.findViewById(R.id.recyclerView);
        mViewPager = view.findViewById(pager);
        loading();
        return view;
    }


    public void loading(){
        RequestParams params = new RequestParams("http://1633.applinzi.com/public/?s=App.Course.Getcourse");
        x.http().get(params, new Callback.CommonCallback<String>() {
            @Override
            public void onSuccess(String result) {
                Gson gson = new Gson();
                CourseModel courseModel = gson.fromJson(result,CourseModel.class);
                if(courseModel != null && courseModel.data != null && courseModel.data.courselist != null){
                    courses = courseModel.data.courselist;
                }
                GridLayoutManager gridLayoutManager = new GridLayoutManager(getActivity(),2);
                mRecyclerView.setLayoutManager(gridLayoutManager);

                mRecyclerView.setAdapter(new MyAdatper());

                //准备资源
                ArrayList<String> banner = courseModel.data.banner;

                ImageView imageView1 = new ImageView(getActivity());
                x.image().bind(imageView1,banner.get(banner.size()-1));
                mViewPagerImgs.add(imageView1);

                for(int i = 0; i<banner.size() ; i++){
                    ImageView imageView = new ImageView(getActivity());
                    x.image().bind(imageView,banner.get(i));
                    mViewPagerImgs.add(imageView);

                }
                ImageView imageView2 = new ImageView(getActivity());
                x.image().bind(imageView2,banner.get(0));
                mViewPagerImgs.add(imageView2);

                mViewPager.setAdapter(new MyViewPagerAdatper());
                mViewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
                    @Override
                    public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

                    }

                    @Override
                    public void onPageSelected(int position) {
                        curent = position;
                    }

                    @Override
                    public void onPageScrollStateChanged(int state) {
                        if(state != ViewPager.SCROLL_STATE_IDLE){
                            return;
                        }
                        if(curent == 0){
                            mViewPager.setCurrentItem(mViewPagerImgs.size()-2,false);
                        }else if(curent == mViewPagerImgs.size()-1){
                            mViewPager.setCurrentItem(1,false);
                        }
                    }
                });

                mViewPager.setCurrentItem(1);
                handler.sendEmptyMessageDelayed(1,2000);
                Log.i("LoginActivity","result:"+result);
            }

            @Override
            public void onError(Throwable ex, boolean isOnCallback) {

            }

            @Override
            public void onCancelled(CancelledException cex) {

            }

            @Override
            public void onFinished() {

            }
        });
    }

    public class MyViewPagerAdatper extends PagerAdapter{

        @Override
        public int getCount() {
            return mViewPagerImgs.size();
        }

        @Override
        public boolean isViewFromObject(View view, Object object) {
            return view == object;
        }

        @Override
        public Object instantiateItem(ViewGroup container, int position) {
            ImageView imageView = mViewPagerImgs.get(position);
            container.addView(imageView);
            return imageView;
        }

        @Override
        public void destroyItem(ViewGroup container, int position, Object object) {
           container.removeView(mViewPagerImgs.get(position));
        }
    }

    public class MyAdatper extends RecyclerView.Adapter<MyViewHolder>{

        @Override
        public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
            View view = View.inflate(getActivity(),R.layout.item_course,null);
            MyViewHolder myViewHolder = new MyViewHolder(view);
            return myViewHolder;
        }

        @Override
        public void onBindViewHolder(MyViewHolder holder, int position) {
            final CourseModel.Course course = courses.get(position);
            holder.textView.setText(course.title);
            holder.contentTitleTv.setText(course.titlecontent);
            x.image().bind(holder.imageView,course.icon);
            holder.mRootView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    Intent intent = new Intent(getActivity(), VideoListActivity.class);
                    intent.putExtra("imaurl",course.imgurl);
                    intent.putExtra("des",course.des);
                    intent.putExtra("video",course.videourl);

                    startActivity(intent);
                }
            });
        }

        @Override
        public int getItemCount() {
            return courses.size();
        }
    }
    public class MyViewHolder extends RecyclerView.ViewHolder {
        ImageView imageView;
        TextView textView;
        TextView contentTitleTv;
        LinearLayout mRootView;
        public MyViewHolder(View itemView) {
            super(itemView);
            imageView = itemView.findViewById(R.id.img);
            textView = itemView.findViewById(R.id.title);
            contentTitleTv = itemView.findViewById(R.id.contenttitle);
            mRootView = itemView.findViewById(R.id.rootview);
        }
    }
}
